﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Data;
using KTXD.Entities;
using System.Data;

namespace UserKTXD.DCTEDU_.QuanLy
{
    public partial class QLChuyenMuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCatalog.BindLoaiChuyenMuc(cboLoaiChuyenMuc);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                Pager2.CurrentIndex = 1;
                Bind_Data1(Pager2.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (cboLoaiChuyenMuc.SelectedValue != "0")
                if (cboSubChuyenMuc.SelectedValue == "")
                    WhereClause += " AND TieuDe like N'%" + Common.StandardString(txtTieuDe.Text) + "%' and IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue;
                else
                    WhereClause += " AND TieuDe like N'%" + Common.StandardString(txtTieuDe.Text) + "%' and IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue + " and IdSubChuyenMuc=" + cboSubChuyenMuc.SelectedValue;
            if (Common.StandardString(txtTieuDe.Text).Length > 0)
                WhereClause += " AND TieuDe like N'%" + Common.StandardString(txtTieuDe.Text) + "%'";
            DataSet dsThongBao = DataRepository.Provider.ExecuteDataSet("ChuyenMucCapHai_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsThongBao.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsThongBao.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }

        protected void Bind_Data1(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (cboLoaiChuyenMuc.SelectedValue != "0")
                WhereClause += " AND TieuDe like N'%" + Common.StandardString(txtTieuDe.Text) + "%' and IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue;
            DataSet dsThongBao = DataRepository.Provider.ExecuteDataSet("ChuyenMuc_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager2.PageSize);
            grvChuyenMuc.DataSource = dsThongBao.Tables[0];
            grvChuyenMuc.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsThongBao.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager2.ItemCount = TotalRows;
            if (TotalRows <= Pager2.PageSize)
                Pager2.Visible = false;
            else
                Pager2.Visible = true;
        }

        protected void Pager2_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager2.CurrentIndex = currnetPageIndex;
            Bind_Data1(currnetPageIndex);
        }

        protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                Pager2.CurrentIndex = 1;
                Bind_Data1(Pager2.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("Admin/QuanLy/QLChuyenMuc.aspx.cs", "cmdTimKiem_Click", ex.ToString());
            }
        }

        protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
        {
            txtTieuDe.Text = string.Empty;
            cboLoaiChuyenMuc.SelectedValue = "0";
        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                    e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                    HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                    e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_TinTuc('" + H_IdCatalog.Value + "');");
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("Admin/QuanLy/QLChuyenMuc.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
            }
        }

        protected void gridCatalog2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                    e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                    HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                    e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_ChuyenMuc('" + H_IdCatalog.Value + "');");
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("Admin/QuanLy/QLChuyenMuc.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
            }
        }

        protected void cmdHieuChinh_Click(object sender, ImageClickEventArgs e)
        {
            HiddenField H_IdCatalog = null;
            for (int i = 0; i < gridCatalog.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                    break;
                }
            }
            if (H_IdCatalog != null)
            {
                Response.Redirect("~/Admin/QuanLy/CNChuyenMuc.aspx?id=" + H_IdCatalog.Value);
            }

            HiddenField H_IdCatalog1 = null;
            for (int i = 0; i < grvChuyenMuc.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvChuyenMuc.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    H_IdCatalog = (HiddenField)grvChuyenMuc.Rows[i].Cells[0].FindControl("H_IdCatalog");
                    break;
                }
            }
            if (H_IdCatalog1 != null)
            {
                Response.Redirect("~/Admin/QuanLy/CNChuyenMuc.aspx?id1=" + H_IdCatalog1.Value);
            }
        }

        protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < gridCatalog.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                    DataRepository.ChuyenMucCapHaiProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));
                }
            }
            for (int i = 0; i < grvChuyenMuc.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvChuyenMuc.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    HiddenField H_IdCatalog = (HiddenField)grvChuyenMuc.Rows[i].Cells[0].FindControl("H_IdCatalog");
                    DataRepository.ChuyenMucProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));
                }
            }
            Bind_Data(Pager1.CurrentIndex);
            Bind_Data1(Pager2.CurrentIndex);
        }

        protected void cboLoaiChuyenMuc_Click(object sender, EventArgs e)
        {
            TList<SubChuyenMuc> TList_Catalogs = DataRepository.SubChuyenMucProvider.Find("IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue);
            cboSubChuyenMuc.DataSource = TList_Catalogs;
            cboSubChuyenMuc.DataTextField = "TenSubChuyenMuc";
            cboSubChuyenMuc.DataValueField = "Id";
            cboSubChuyenMuc.DataBind();
        }

        public string GetLoaiChuyenMuc(object Char)
        {
            int idTinTuc = Convert.ToInt32(Char.ToString());
            using (LoaiChuyenMuc entLoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(idTinTuc))
            {
                return entLoaiChuyenMuc.TenLoaiChuyenMuc;
            }
        }

        public string GetSubChuyenMuc(object Char)
        {
            int idSubChuyenMuc = Convert.ToInt32(Char.ToString());
            using (SubChuyenMuc entSubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(idSubChuyenMuc))
            {
                return entSubChuyenMuc.TenSubChuyenMuc;
            }
        }

        public string GetTenThanhVien(object Char)
        {
            int idThanhVien = Convert.ToInt32(Char.ToString());
            using (ThanhVien entThanhVien = DataRepository.ThanhVienProvider.GetById(idThanhVien))
            {
                return entThanhVien.HoTen;
            }
        }

        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }
    }
}